<?php

class MKT_Helper {
    
    /**
     * Get product image gallery
     * @global type $post
     * @param type $product_id
     * @return type
     * 
     * @author DungLH
     */
    public static function getProductImageGallery($product_id = '') {
        if (!is_numeric($product_id)) {
            global $post;
            $product_id = $post->ID;
        }

        $attachments = array();
        if (metadata_exists('post', $product_id, '_product_image_gallery')) {
            $product_image_gallery = get_post_meta($product_id, '_product_image_gallery', true);
        } else {
            // Backwards compat
            $attachment_ids = get_posts('post_parent=' . $product_id . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image&fields=ids&meta_key=_woocommerce_exclude_image&meta_value=0');
            $attachment_ids = array_diff($attachment_ids, array(get_post_thumbnail_id()));
            $product_image_gallery = implode(',', $attachment_ids);
        }

        $attachments = array_filter(explode(',', $product_image_gallery));

        if ($attachments) {
            foreach ($attachments as $aid) {
                $gallery[] = wp_get_attachment_url($aid);
            }
        }

        return $attachments;
    }

    /**
     * get all imagesid of current product except post_thumbnail_id
     * @global type $post
     * @param type $product_id
     * @param type $size
     * @return type
     * 
     * @author TuanTQ
     */
    public static function getProductImage($product_id = '', $size='large') {
        if (!is_numeric($product_id)) {
            global $post;
            $product_id = $post->ID;
        }

        $attachments = array();
        if (metadata_exists('post', $product_id, '_product_image_gallery')) {
            $product_image_gallery = get_post_meta($product_id, '_product_image_gallery', true);
        } else {
            // Backwards compat
            $attachment_ids = get_posts('post_parent=' . $product_id . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image&fields=ids&meta_key=_woocommerce_exclude_image&meta_value=0');
            $attachment_ids = array_diff($attachment_ids, array(get_post_thumbnail_id()));
            $product_image_gallery = implode(',', $attachment_ids);
        }

        $attachments = array_filter(explode(',', $product_image_gallery));

        return $attachments;
    }
    
}

